// packageB/member/enterprise/enterprise_store/enterprise_store.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    optype: 0,

    // 市级二开数据组
    enterprise_city: false,
    infoData: [],
    order_sum_order: "desc",
    reward_sum_order: "desc",
    district_id: "",
    area_list: [],
    show: false,
    active: 1,
    store_income_show: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 2) {
      this.setData({
        optype: options.type
      })
      this.getData(); //获取数据
      return
    }
    if (options.active && options.active == 'enterprise_city' || options.type == 1) {
      this.setData({
        enterprise_city: options.type == 1 ? false : true,
        optype: options.type == 1 ? 1 : 0
      })
      if(options.type == 1){
        this.setData({
          store_income_show:0
        })
      }
      this.enterprise_city_data();
      return
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onConfirm(e) {
    console.log(e)
    this.setData({
      district_id: e.detail.value.id,
      page: 1,
      show: false
    })
    this.enterprise_city_data();
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  order_sum_orderTap() {
    if (this.data.order_sum_order == "desc") {
      this.data.order_sum_order = "asc";
    } else {
      this.data.order_sum_order = "desc";
    }
    this.setData({
      page: 1,
      order_sum_order: this.data.order_sum_order
    })
    this.enterprise_city_data();
  },
  reward_sum_orderTap() {
    if (this.data.reward_sum_order == "desc") {
      this.data.reward_sum_order = "asc";
    } else {
      this.data.reward_sum_order = "desc";
    }
    this.setData({
      page: 1,
      reward_sum_order: this.data.reward_sum_order
    })
    this.enterprise_city_data();
  },
  TabChange(e) {
    console.log(e)
    this.setData({
      active: e.detail.name,
      page: 1
    })
    this.enterprise_city_data();
  },
  async enterprise_city_data() {
    wx.showLoading({
      title: '加载中',
    })
    let urlstr = app.getNetAddresss("plugin.city-merchant.frontend.index.storeList");
    let json = {
      time_type: this.data.active,
      order_sum_order: this.data.order_sum_order,
      reward_sum_order: this.data.reward_sum_order,
      page: this.data.page
    };
    if (this.data.optype == 1) {
      urlstr = app.getNetAddresss("plugin.merchant.frontend.store.list.storeListByStaff");
    } else {
      json.district_id = this.data.district_id;
    }
    let {
      data
    } = await app._postNetWork({
      url: urlstr,
      data: json
    })
    if (data.result) {
      if (this.data.optype == 1) {
        this.setData({
          store_income_show: data.data.store_income_show,
          total_page: data.data.store_list.last_page,
          infoData: data.data.store_list.data
        })
      } else {
        this.setData({
          infoData: data.data.data,
          total_page: data.data.last_page,
          area_list: data.data.area_list
        })
      }

      this.data.isLoadMore = true;
      wx.hideLoading();
    } else {
      app.tips(data.msg);
      wx.hideLoading();
    }
  },
  showTap() {
    this.setData({
      show: true
    })
  },
  //获取数据
  getData() {
    let json = {
      page: 1
    };
    let urlStr = '';
    if (this.data.optype == 1) {
      urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeListByStaff');
    } else if (this.data.optype == 2) {
      urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeList');
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            list: resdata.data.store_list.data,
            total_page: resdata.data.store_list.last_page
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  toShop(e) {
    let id = e.currentTarget.dataset.storeid;
    wx.navigateTo({
      url: '/packageC/o2o/HomeSeller/HomeSeller' + '?store_id=' + id
    });
  },
  //获取更多数据
  getMoreData() {
    let json = {
      page: this.data.page
    };
    let urlStr = '';
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      if (this.data.optype == 1) {
        urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeListByStaff');
      } else if (this.data.optype == 2) {
        urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeList');
      }
      if (this.data.enterprise_city) {
        urlStr = app.getNetAddresss("plugin.city-merchant.frontend.index.storeList");
        json.time_type = this.data.active;
        json.order_sum_order = this.data.order_sum_order;
        json.reward_sum_order = this.data.reward_sum_order;
        json.district_id = this.data.district_id;
        json.page = this.data.page;
      }
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            if (this.data.enterprise_city) {
              this.data.isLoadMore = true;
              this.setData({
                infoData: this.data.infoData.concat(resdata.data.data)
              })
              return
            }
            var myData = resdata.data.store_list.data;
            this.setData({
              list: this.data.list.concat(myData)
            });
            if (this.data.page < resdata.data.store_list.last_page) {
              this.data.isLoadMore = true;
            }
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});